Step 1: Installation
composer require intervention/image
config/app.php
return [
......
$provides => [
......
......,
'Intervention\Image\ImageServiceProvider'
],
$aliases => [
.....
.....,
'Image' => 'Intervention\Image\Facades\Image'
]
]
Step 2: Route and Controller
app/Http/routes.php
Route::get('resizeImage', 'ImageController@resizeImage');
Route::post('resizeImagePost',['as'=>'resizeImagePost','uses'=>'ImageController@resizeImagePost']);
app/Http/Controllers/ImageController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Image;
class ImageController extends Controller
{
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function resizeImage()
{
return view('resizeImage');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function resizeImagePost(Request $request)
{
$this->validate($request, [
'title' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$image = $request->file('image');
$input['imagename'] = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/thumbnail');
$img = Image::make($image->getRealPath());
$img->resize(100, 100, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath.'/'.$input['imagename']);
$destinationPath = public_path('/images');
$image->move($destinationPath, $input['imagename']);
$this->postImage->add($input);
return back()
->with('success','Image Upload successful')
->with('imageName',$input['imagename']);
}
}
Orientation
$thumbImage = Image::make($image->path());
$thumbImage->orientate()
->fit(500, 500, function ($constraint) {
$constraint->upsize();
$constraint->aspectRatio();
})
->encode('jpg', 40);
Image compression
$imagick = new Imagick();
$imagick->setResolution(200, 200);
$imagick->readimage($image->path());
$imagick->setImageFormat('jpg');
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality(60);
$imagick->writeImage('filename-here.jpg');
Image Resizing aspect ratio
// Path to the original image
$originalImagePath = ‘path/to/original/image.jpg’;
// Create Imagick object
$image = new Imagick($originalImagePath);
// Get the original dimensions
$originalWidth = $image->getImageWidth();
$originalHeight = $image->getImageHeight();
// Set the maximum dimensions for the resized image
$maxWidth = 800;
$maxHeight = 600;
// Calculate the new dimensions while maintaining the aspect ratio
if ($originalWidth > $maxWidth || $originalHeight > $maxHeight) {
$aspectRatio = $originalWidth / $originalHeight;
$newWidth = $maxWidth;
$newHeight = $newWidth / $aspectRatio;
if ($newHeight > $maxHeight) {
$newHeight = $maxHeight;
$newWidth = $newHeight * $aspectRatio;
}
// Resize the image
$image->resizeImage($newWidth, $newHeight, Imagick::FILTER_LANCZOS, 1);
}
// Set the image format to JPEG
$image->setImageFormat(‘jpeg’);
// Path to save the resized image
$resizedImagePath = ‘path/to/save/resized/image.jpg’;
// Save the resized image
$image->writeImage($resizedImagePath);
// Clear resources
$image->clear();
$image->destroy();
echo “Image resized and saved successfully.“;